angular.module('detailApp', []).controller("detailController", function ($scope, $http) {
    if ($("#productId").val()) {
        $scope.detailFlag = true;//显示详情页
        $scope.showIndex = false;
/*        $http.get(vPath + "/home/findProductInfo.json?id=" + $("#productId").val()).success(function (data) {
            $scope.product = data || [];
        });*/
    }

    $http.get(vPath + "/home/findAllGiftType.json").success(function (data) {
        $scope.giftTypes = data || [];
    });

    $http.get(vPath + "/home/findProductPage.json").success(function (data) {
        $scope.giftList = data["list"] || [];
        $scope.pageCount = data["pageCount"];
        $scope.currentPage = data["currentPage"];
        $scope.indexClass = "chosen-tap";
    });

    if ($("#customerId").val()) {
        $http.get(vPath + "/home/findCasePage.json?customerId=" + $("#customerId").val()).success(function (data) {
            $scope.caseList = data["list"] || [];
            $scope.casePageCount = data["pageCount"];
            $scope.caseCurrentPage = data["currentPage"];
        });
    }

    $scope.searchByName = function () {
        if($scope.detailFlag){//如果在详情页，默认礼物查询
            $scope.showIndex = true;
        }
        $scope.detailFlag = false;//隐藏详情页
        if ($scope.showIndex) {//礼物查询
            $scope.searchType = "name";
            var currentPage = $scope.currentPage || 1;
            $.post(vPath + "/home/findProductPage.json", {search_LIKE_name: $scope.giftName, pageNum: currentPage}).
                success(function (data) {
                    $scope.giftList = data["list"] || [];
                    $scope.pageCount = data["pageCount"];
                    $scope.currentPage = data["currentPage"];
                    $scope.$apply();
                });
        } else {//案例查询
            var currentPage = $scope.caseCurrentPage || 1;
            $.post(vPath + "/home/findCasePage.json", {customerId: $("#customerId").val(), caseName: $scope.giftName, pageNum: currentPage}).
                success(function (data) {
                    $scope.caseList = data["list"] || [];
                    $scope.casePageCount = data["pageCount"];
                    $scope.caseCurrentPage = data["currentPage"];
                    $scope.$apply();
                });
        }
    };

    $scope.searchByType = function (id) {
        $scope.searchType = "type";
        $scope.typeId = id;
        var currentPage = $scope.currentPage || 1;
        $.post(vPath + "/home/findProductPage.json", {"search_EQ_twoLevelType.id": id, pageNum: currentPage}).
            success(function (data) {
                $scope.giftList = data["list"] || [];
                $scope.pageCount = data["pageCount"];
                $scope.currentPage = data["currentPage"];
                $scope.$apply();
            });
    }

    $scope.searchPageByType = function () {
        var currentPage = $scope.currentPage || 1;
        $.post(vPath + "/home/findProductPage.json", {"search_EQ_twoLevelType.id": $scope.typeId, pageNum: currentPage}).
            success(function (data) {
                $scope.giftList = data["list"] || [];
                $scope.pageCount = data["pageCount"];
                $scope.currentPage = data["currentPage"];
                $scope.$apply();
            });
    }

    $scope.searchPagination = function (pageNum) {
        if (pageNum <= 0) {
            $scope.currentPage = 1;
        } else if (pageNum >= $scope.pageCount) {
            $scope.currentPage = $scope.pageCount;
        } else {
            $scope.currentPage = pageNum;
        }
        if ($scope.searchType) {
            if ($scope.searchType == "name") {
                $scope.searchByName();
            } else {
                $scope.searchPageByType();
            }
        } else {
            $scope.searchByName();
        }
    }

    $scope.searchCasePagination = function (pageNum) {
        if (pageNum <= 0) {
            $scope.caseCurrentPage = 1;
        } else if (pageNum >= $scope.casePageCount) {
            $scope.caseCurrentPage = $scope.casePageCount;
        } else {
            $scope.caseCurrentPage = pageNum;
        }
        $scope.searchByName();
    }

    $scope.changeTab = function (flag) {
        $scope.showIndex = flag;
        $scope.detailFlag=false;
        $scope.giftName="";
        $scope.typeId="";
        if (flag) {
            $scope.indexClass = "chosen-tap";
            $scope.caseClass = "";
            $http.get(vPath + "/home/findProductPage.json").success(function (data) {
                $scope.giftList = data["list"] || [];
                $scope.pageCount = data["pageCount"];
                $scope.currentPage = data["currentPage"];
                $scope.indexClass = "chosen-tap";
            });
        } else {
            $scope.caseClass = "chosen-tap";
            $scope.indexClass = "";
            $http.get(vPath + "/home/findCasePage.json?customerId=" + $("#customerId").val()).success(function (data) {
                $scope.caseList = data["list"] || [];
                $scope.casePageCount = data["pageCount"];
                $scope.caseCurrentPage = data["currentPage"];
            });
        }
    }
});

